package com.csc.contact.checkbill.controller;


import com.alibaba.fastjson.JSON;
import com.csc.contact.checkbill.entity.*;
import com.csc.contact.checkbill.kafkaconfig.KafkaProducer;
import com.csc.contact.checkbill.service.*;
import com.csc.contact.checkbill.utiltools.ftp.FtpConfig;
import com.csc.contact.checkbill.utiltools.ftputils.FtpReadFileImpl;
import com.csc.contact.checkbill.utiltools.sftputil.SftpUtil;
import com.hollycrm.hollybeacons.system.util.DateUtils;
import com.hollycrm.hollybeacons.system.util.StringUtils;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpException;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileWriter;
import java.util.*;

/**
 * 解压文件
 * Created by zhanglei
 * Create Date 2019-06-17
 */
public class ReadFileFromFTPTask implements Runnable {
    private static Logger logger = Logger.getLogger(ReadFileFromFTPTask.class);
    public static final int BATCHSIZE = 1000;
    //平账成功记录数
    private int successCount;
    //平账成功金额数
    private int successAmount;
    //平账失败记录数
    private int failCount;
    //平账失败金额数
    private int failAmount;
    //平账一致记录数
    private int unityCount;
    //平账一致金额数
    private int unityAmount;

    List<ReconciliationInfo> reconciliationInfos = new ArrayList<>();
    private ReconciliationServiceImpl reconciliationService;
    private UrpChargeinfoServiceImpl urpChargeinfoService;
    private CheckLogServiceImpl checklogService;
    private CheckDiffServiceImpl checkDiffService;
    private UrpProvinceinfoServiceImpl urpProvinceinfoService;
    private KafkaProducer kafkaProducer;

    private String errCount;


    private Map<Integer, Integer> abNormalMap = new HashMap<>();

    public ReadFileFromFTPTask(ReconciliationServiceImpl reconciliationService, UrpChargeinfoServiceImpl urpChargeinfoService,
                               CheckLogServiceImpl checklogService, CheckDiffServiceImpl checkDiffService, UrpProvinceinfoServiceImpl
                                       urpProvinceinfoService, KafkaProducer kafkaProducer) {
        this.checkDiffService = checkDiffService;
        this.checklogService = checklogService;
        this.reconciliationService = reconciliationService;
        this.urpChargeinfoService = urpChargeinfoService;
        this.urpProvinceinfoService = urpProvinceinfoService;
        this.kafkaProducer = kafkaProducer;
    }

    @Override
    public void run() {
        Boolean ishavefile = false;
        Boolean isReadSuccess = true;
        errCount = urpChargeinfoService.getErrCount();
        logger.info("获取到的阈值====" + errCount);
        logger.info("SFTP获取文件任务开始执行...");
        SftpUtil sftpUtil = new SftpUtil();
        String path = FtpConfig.config.get("path_start");
        logger.info("path路径=====" + path);
        ChannelSftp sftp = connectServer(sftpUtil);
        while (!sftp.isConnected()){
            sftp = connectServer(sftpUtil);
            if(sftp.isConnected()){
                break;
            }
        }
        logger.info("sftp对象====" + sftp);
        try {
            String billDate = "";
            String fileName = "";
            //获取对账日志信息表中的未对账成功的记录
            List<CheckLog> checkLogs = checklogService.queryFailCheckInfo();
            logger.info("获取对账日志信息表中的未对账成功的记录====" + checkLogs);
            if (checkLogs == null || checkLogs.size() == 0) {
                String maxDate = checklogService.queryMaxDate();
                logger.info("对账日志表中最大的日期====" + maxDate);
                Date newDate = DateUtils.add(DateUtils.parse(maxDate, "yyyyMMdd"), Calendar.DAY_OF_MONTH, 1);
                billDate = DateUtils.format(newDate, DateUtils.FORMAT_DATE_YYYYMMDD);
                //对账日志信息表中的结果都成功，开始对新一天的记录信息，插入日志记录信息
            } else {
                billDate = checkLogs.get(0).getCheckDate();
            }
            //  切换到文件目录下
            if (sftpUtil.openDir(path, sftp)) {
                List<String> files = sftpUtil.getFiles(sftp, path);
                logger.info("获取的文件对象====" + files.size());
                if (files.size() > 0) {
                    logger.info("读取文件前清除数据库中数据：执行sql：truncate  tbl_cscc_URP_reconciliation");
                    boolean res = reconciliationService.trucateTable();
                    //查询已割接省份编码
                    List<UrpProvinceinfo> queryAllY = urpProvinceinfoService.provinceinfoQueryY();
                    List<String> queryY = new ArrayList<>();
                    for(UrpProvinceinfo urpinfo : queryAllY){
                        queryY.add(urpinfo.getPiProvinceid()+"");
                    }
                    queryY.add("93");
                    for (String name : files) {
                        logger.info("文件名称=====" + name);
                        //如果目录存在
                        String fileNamePrefix = FtpConfig.config.get("file_name_prefix");
                        //遍历所有文件名中带已割接省份编码的文件
                        for(String proY : queryY) {
                            fileName = fileNamePrefix + proY + billDate;
                            logger.info("拼接的文件名称====" + fileName);
                            if (name.indexOf(fileName) > -1) {
                                logger.info("开始读取文件：" + path + name);
                                if (res) {
                                    logger.info("执行sql：truncate  tbl_cscc_URP_reconciliation成功，数据清除完毕！");
                                            isReadSuccess = sftpUtil.readFile(name,
                                            //文件名
                                            FtpConfig.config.get("ftp.encode"),                         //编码
                                            path + name, //文件路径
                                            FtpReadFileImpl.getFtpReadFileIns(),                    //文件读取实现
                                            reconciliationInfos,                                 //入库对象list
                                            reconciliationService,                               //入库操作实现类
                                            queryY,                                             //已割接编码
                                            proY                                                //当前文件省编码
                                    );
                                    try {
                                        if (!isReadSuccess) {
                                            logger.error("sftp获取文件失败！");
                                            sftpUtil.close(); //断开连接
                                            /**
                                             * 这里重新创建连接，要不然会出现断开的现象，会读不到文件
                                             */
                                            sftpUtil = new SftpUtil(); //重新创建连接
                                            while (!isReadSuccess) {  //写入失败的时候重新连接并写入
                                                while (!sftp.isConnected()){
                                                    sftp = connectServer(sftpUtil);
                                                    if(sftp.isConnected()){
                                                        break;
                                                    }
                                                }
                                                isReadSuccess = sftpUtil.readFile(name,
                                                        //文件名
                                                        FtpConfig.config.get("ftp.encode"),                             //编码
                                                        path + name, //文件路径
                                                        FtpReadFileImpl.getFtpReadFileIns(), reconciliationInfos,
                                                        //入库对象list
                                                        reconciliationService,                                //入库操作实现类
                                                        queryY,                                             //已割接编码
                                                        proY                                                //当前文件省编码
                                                );
                                                Thread.sleep(1000);
                                            }
                                        }
                                    } catch (InterruptedException e) {
                                        logger.info(" InterruptedException error信息=====", e);
                                    }
                                } else {
                                    logger.info("执行sql：truncate  table tbl_cscc_URP_reconciliation 失败！");
                                }
                                if(isReadSuccess){
                                    sftpUtil.rename(name, path + "bak/" + name, "sn");
                                    //有对账文件
                                    ishavefile = true;
                                }

                            }
                        }
                    }
                    sftpUtil.close();
                    //如果没文件，直接结束
                    if(!ishavefile){
                        logger.info("------------当前逻辑日无对账文件-----------");
                        return;
                    }
                    logger.info("文件导入成功，关闭sftp连接！");
                    compareRecord(checkLogs, billDate);
                    String resultName = FtpConfig.config.get("result_path") + fileName + ".check.bak";
                    String uploadPath = FtpConfig.config.get("upload_path");
                    exportReceipt(billDate, resultName);
                    File resultFile = new File(resultName);
                    if (resultFile.exists()) {
                        ChannelSftp sftp1 = connectServer(sftpUtil);;
                        while (!sftp1.isConnected()){
                            sftp1 = connectServer(sftpUtil);
                            if(sftp1.isConnected()){
                                break;
                            }
                        }
                        Boolean flag = sftpUtil.upload(uploadPath, resultName, "sn");
                        while (!flag) {
                            sftpUtil.close();
                            sftpUtil = new SftpUtil();
                            while (!sftp1.isConnected()){
                                sftp1 = connectServer(sftpUtil);
                                if(sftp1.isConnected()){
                                    break;
                                }
                            }
                            flag = sftpUtil.upload(uploadPath, resultName, "sn");
                        }
                        sftpUtil.openDir(uploadPath, sftp1);
                        sftpUtil.delete(uploadPath, fileName + ".check");
                        sftpUtil.rename(fileName + ".check.bak", fileName + ".check", "sn");
                        delete(FtpConfig.config.get("result_path"), fileName + ".check.bak");
                    } else {
                        logger.info("回执文件不存在。");
                    }
                    sftpUtil.close();
                    logger.info("回执文件上传结束，关闭sftp连接！");
                    writeHkFile(billDate, sftpUtil);
                } else {
                    logger.info("文件目录下没有对账文件===" + path);
                }
            }
            sftpUtil.close();
            logger.info("sftp关闭！");
            abNormalMap.clear();
        } catch (SftpException e) {
            logger.info("SftpException error信息======", e);
        }

    }

    /**
     * 删除本地生成的临时文件
     *
     * @param directory
     * @param deleteFile
     */
    public void delete(String directory, String deleteFile) {
        try {
            File file = new File(directory + deleteFile);
            if (!file.exists()) {
                logger.info("删除的文件不存在" + file.getName());
            }
            if (file.isFile()) {
                boolean flag = file.delete();
                if (flag) {
                    logger.info("文件删除成功====");
                } else {
                    logger.info("文件删除失败====");
                }
            }
        } catch (Exception e) {
            logger.info("临时文件删除异常", e);
        }

    }


    /**
     * 输出给号卡文件
     *
     * @param billDate
     * @param sftpUtil
     */
    public void writeHkFile(String billDate, SftpUtil sftpUtil) {
        FileWriter fw = null;
        FileWriter hkzsfw = null;
        FileWriter fws = null;
        FileWriter hkzsfws = null;
        try {
            ChannelSftp sftp = connectHkServer(sftpUtil);
            while (!sftp.isConnected()){
                sftp = connectHkServer(sftpUtil);
                if(sftp.isConnected() ){
                    break;
                }
            }
            List<CheckLog> checkLogs = checklogService.querySucCheckInfo(billDate);
            String resultPath = FtpConfig.config.get("result_path");
            String hkFilePrefix = FtpConfig.config.get("hkfile_name_prefix");
            String hkzsFilePrefix = FtpConfig.config.get("hkzsfile_name_prefix");
            String hkResultFileNameTmp = "";
            String hkFinalResultFileName = "";
            String hkzsResultFileNameTmp = "";
            String hkzsFinalResultFileName = "";
            String hkUploadPath = FtpConfig.config.get("hk_upload_path");
            String hkzsUploadPath = FtpConfig.config.get("hkzs_upload_path");
            //备份对账文件和充值记录同步文件
            String bakUploadPath = FtpConfig.config.get("bak_upload_path");
            List<String> logPros = new ArrayList<>();
            if (checkLogs != null && checkLogs.size() != 0) {
                for (CheckLog checkLog : checkLogs) {
                    String provinceCode = checkLog.getProvideCode();
                    logPros.add(provinceCode);
                    hkResultFileNameTmp = hkFilePrefix + billDate + "." + provinceCode + ".bak";
                    hkFinalResultFileName = hkFilePrefix + billDate + "." + provinceCode;
                    hkzsResultFileNameTmp = hkzsFilePrefix + billDate + "." + provinceCode + ".bak";
                    hkzsFinalResultFileName = hkzsFilePrefix + billDate + "." + provinceCode;
                    logger.info("对账文件名称" + hkResultFileNameTmp + "|||||" + hkFinalResultFileName);
                    logger.info("充值记录同步文件名称" + hkzsResultFileNameTmp + "|||||" + hkzsFinalResultFileName);
                    UrpProvinceinfo urpProvinceinfo = urpProvinceinfoService.provinceinfoQueryById(provinceCode);
                    if (urpProvinceinfo != null) {
                        if ("Y".equals(urpProvinceinfo.getPiIfmigrate())) {
                            fw = new FileWriter(resultPath + hkResultFileNameTmp, true);
                        }
                        hkzsfw = new FileWriter(resultPath + hkzsResultFileNameTmp, true);
                    }
                    Map params = new HashMap();
                    params.put("checkDay", billDate);
                    params.put("provinceCode", provinceCode);
                    List<UrpChargeinfo> urpChargeinfos = urpChargeinfoService.queryChargeInfoByEntryProvince(params);
                    if (urpChargeinfos != null && urpChargeinfos.size() != 0) {
                        String txtbasic = "";
                        String txthkzs = "";
                        for (UrpChargeinfo urpChargeinfo : urpChargeinfos) {
                            txtbasic = new StringBuffer(urpChargeinfo.getCgiRequesttime()).append(",").append(urpChargeinfo
                                    .getCgiSerialno()).append(",").append(urpChargeinfo.getCgiMoney() * 10).append(",").append
                                    (urpChargeinfo.getCgiEntryprovince()).append(",").append(urpChargeinfo.getCgiAccnum()).append(",").
                                    append(urpChargeinfo.getCiCardnum()).append(",").append(urpChargeinfo.getCgiEntrytype()).append(",")
                                    .append(urpChargeinfo.getCgiCardprovince()).append(",").append(urpChargeinfo.getCgiCardcity()).append
                                            (",").append(urpChargeinfo.getCgiAccbasetype()).append(",").append(urpChargeinfo
                                            .getCgiSystemcode()).append(",").append(urpChargeinfo.getCgiAccprovince()).append(",").append
                                            (urpChargeinfo.getCgiAcccity()).append(",").append(urpChargeinfo.getCgiIsgift()).append(",")
                                    .append(urpChargeinfo.getCgiCardnet()).append("\r\n").toString();
                            txthkzs = new StringBuffer(urpChargeinfo.getCgiSerialno()).append(",").append(billDate).append(",")
                                    .append(urpChargeinfo.getCgiAccnum()).append(",").append(urpChargeinfo.getCgiAccbasetype()).append(",")
                                    .append(urpChargeinfo.getCiCardnum()).append(",").append(urpChargeinfo.getCgiMoney() * 10).append(", ,")
                                    .append(urpChargeinfo.getCgiCardprovince()).append(",").append(urpChargeinfo.getCgiCardcity()).append(",")
                                    .append(urpChargeinfo.getCgiAccprovince()).append(",").append(urpChargeinfo.getCgiAcccity()).append(",")
                                    .append(urpChargeinfo.getCgiEntryprovince()).append(",").append(urpChargeinfo.getCgiEntrytype()).append(",")
                                    .append(urpChargeinfo.getCgiRequesttime()).append(",").append(urpChargeinfo.getCgiSystemcode()).append(",")
                                    .append(urpChargeinfo.getCgiIsgift()).append(",").append(urpChargeinfo.getCgiCardnet()).append("\r\n").toString();

                            hkzsfw.write(txthkzs);
                            if ("Y".equals(urpProvinceinfo.getPiIfmigrate())) {
                                fw.write(txtbasic);
                            }
                        }
                        int chargeSuccesscount = urpChargeinfoService.queryCountByProvince(params);
                        if ("Y".equals(urpProvinceinfo.getPiIfmigrate())) {
                            int chargeSuccessMenoy = urpChargeinfoService.querySumMoneyByProvince(params);
                            String txtEnd = new StringBuffer("TOTAL_COUNT=").append(chargeSuccesscount).append(",").append("TOTAL_MONEY=")
                                    .append(chargeSuccessMenoy * 10).toString();
                            fw.write(txtEnd);
                            fw.flush();
                            fw.close();
                            logger.info("上传对账文件名称======" + resultPath + hkResultFileNameTmp);
                            boolean czflag = sftpUtil.upload(hkUploadPath, resultPath + hkResultFileNameTmp, "hk");
                            //对账文件备份
                            boolean czflagbak = sftpUtil.upload(bakUploadPath, resultPath + hkResultFileNameTmp, "hk");
                            while (!czflag || !czflagbak){
                                sftpUtil.close();
                                sftpUtil = new SftpUtil();
                                while (!sftp.isConnected()){
                                    sftp = connectHkServer(sftpUtil);
                                    if(sftp.isConnected() ){
                                        break;
                                    }
                                }
                                czflag = sftpUtil.upload(hkUploadPath, resultPath + hkResultFileNameTmp, "hk");
                                czflagbak = sftpUtil.upload(bakUploadPath, resultPath + hkResultFileNameTmp, "hk");
                            }
                            sftpUtil.openDir(hkUploadPath, sftp);
                            sftpUtil.rename(hkResultFileNameTmp, hkFinalResultFileName, "hk");
                            delete(resultPath, hkResultFileNameTmp);
                        }
                        String hkTxtEnd = new StringBuffer(billDate).append(",").append(chargeSuccesscount).toString();
                        hkzsfw.write(hkTxtEnd);
                        hkzsfw.flush();
                        hkzsfw.close();
                        logger.info("上传充值记录同步文件名称======" + resultPath + hkzsResultFileNameTmp);
                        boolean flag = sftpUtil.upload(hkzsUploadPath, resultPath + hkzsResultFileNameTmp, "hk");
                        //充值记录同步文件备份
                        boolean flagbak = sftpUtil.upload(bakUploadPath, resultPath + hkzsResultFileNameTmp, "hk");
                        while (!flag || !flagbak){
                            sftpUtil.close();
                            sftpUtil = new SftpUtil();
                            while (!sftp.isConnected()){
                                sftp = connectHkServer(sftpUtil);
                                if(sftp.isConnected()){
                                    break;
                                }
                            }
                            flag = sftpUtil.upload(hkzsUploadPath, resultPath + hkzsResultFileNameTmp, "hk");
                            flagbak = sftpUtil.upload(bakUploadPath, resultPath + hkzsResultFileNameTmp, "hk");
                        }
                        sftpUtil.openDir(hkzsUploadPath, sftp);
                        sftpUtil.rename(hkzsResultFileNameTmp, hkzsFinalResultFileName, "hk");
                        delete(resultPath, hkzsResultFileNameTmp);
                    } else {
                        logger.info("======当天对账文件全部为空======");
                        //return;
                    }
                }
                //查询所有省份
                List<UrpProvinceinfo> queryAll = urpProvinceinfoService.provinceinfoQueryAll();
                for(UrpProvinceinfo qAll : queryAll){
                    String allYPro = qAll.getPiProvinceid()+"";
                    //已割接-对账文件 且 （枢纽传给充值中心是空文件 或 未对账的其他省空文件）
                    if ("Y".equals(qAll.getPiIfmigrate()) && !"99".equals(allYPro) &&(logPros.contains("93") || !logPros.contains(allYPro)) ) {
                        fws = new FileWriter(resultPath + hkFilePrefix + billDate + "." + allYPro + ".bak", true);
                        String YEnd = new StringBuffer("TOTAL_COUNT=0,TOTAL_MONEY=0").toString();
                        fws.write(YEnd);
                        fws.flush();
                        fws.close();
                        logger.info("上传对账文件名称======" + resultPath + hkFilePrefix + billDate + "." + allYPro + ".bak");
                        boolean czflag = sftpUtil.upload(hkUploadPath, resultPath + hkFilePrefix + billDate + "." + allYPro + ".bak", "hk");
                        //对账文件备份
                        boolean czflagbak = sftpUtil.upload(bakUploadPath, resultPath + hkFilePrefix + billDate + "." + allYPro + ".bak", "hk");
                        while (!czflag || !czflagbak){
                            sftpUtil.close();
                            sftpUtil = new SftpUtil();
                            while (!sftp.isConnected()){
                                sftp = connectHkServer(sftpUtil);
                                if(sftp.isConnected()){
                                    break;
                                }
                            }
                            czflag = sftpUtil.upload(hkUploadPath, resultPath + hkFilePrefix + billDate + "." + allYPro + ".bak", "hk");
                            czflagbak = sftpUtil.upload(bakUploadPath, resultPath + hkFilePrefix + billDate + "." + allYPro, "hk");
                        }
                        sftpUtil.openDir(hkUploadPath, sftp);
                        sftpUtil.rename(hkFilePrefix + billDate + "." + allYPro + ".bak", hkFilePrefix + billDate + "." + allYPro, "hk");
                        delete(resultPath, hkFilePrefix + billDate + "." + allYPro + ".bak");
                    }
                    //全部-充值记录同步文件 -- 枢纽传给充值中心是空文件 或 未对账的其他省空文件
                    if(!"99".equals(allYPro) && (logPros.contains("93") || !logPros.contains(allYPro))){
                        hkzsfws = new FileWriter(resultPath + hkzsFilePrefix + billDate + "." + allYPro + ".bak", true);
                        String AllEnd = new StringBuffer(billDate).append(",0").toString();
                        hkzsfws.write(AllEnd);
                        hkzsfws.flush();
                        hkzsfws.close();
                        logger.info("上传充值记录同步文件名称======" + resultPath + hkzsFilePrefix + billDate + "." + allYPro + ".bak");
                        boolean flag = sftpUtil.upload(hkzsUploadPath, resultPath + hkzsFilePrefix + billDate + "." + allYPro + ".bak", "hk");
                        //充值记录同步文件备份
                        boolean flagbak = sftpUtil.upload(bakUploadPath, resultPath + hkzsFilePrefix + billDate + "." + allYPro + ".bak", "hk");
                        while (!flag || !flagbak){
                            sftpUtil.close();
                            sftpUtil = new SftpUtil();
                            while (!sftp.isConnected()){
                                sftp = connectHkServer(sftpUtil);
                                if(sftp.isConnected()){
                                    break;
                                }
                            }
                            flag = sftpUtil.upload(hkzsUploadPath, resultPath + hkzsFilePrefix + billDate + "." + allYPro + ".bak", "hk");
                            flagbak = sftpUtil.upload(bakUploadPath, resultPath + hkzsFilePrefix + billDate + "." + allYPro, "hk");
                        }
                        sftpUtil.openDir(hkzsUploadPath, sftp);
                        sftpUtil.rename(hkzsFilePrefix + billDate + "." + allYPro + ".bak", hkzsFilePrefix + billDate + "." + allYPro, "hk");
                        delete(resultPath, hkzsFilePrefix + billDate + "." + allYPro + ".bak");
                    }
                }

                sftpUtil.close();
                logger.info("号卡对账文件上传成功，关闭sftp连接资源！");
            }

        } catch (Exception e) {
            logger.info("输出号卡文件异常======", e);
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                }
                if (hkzsfw != null) {
                    hkzsfw.close();
                }
                if (fws != null) {
                    fws.close();
                }

            } catch (Exception e) {
                logger.info("关闭号卡文件输出流错误======", e);
            }
        }
    }

    /**
     * 插入对账日志信息表
     *
     * @param billDate
     * @param provideList
     */
    private void insertData2CheckLog(String billDate, List<String> provideList, String type) {
        String startTime = DateUtils.format(new Date(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HHMMSS);
        List<CheckLog> checkLogList = new ArrayList<>();
        for (int i = 0; i < provideList.size(); i++) {
            CheckLog checkLog = new CheckLog();
            checkLog.setCheckDate(billDate);
            if ("93".equals(type)) {
                checkLog.setCheckStatus("1");
            } else {
                checkLog.setCheckStatus("0");
            }
            checkLog.setErrorDesc("");
            checkLog.setStartTime(startTime);
            checkLog.setUpdateTime("");
            checkLog.setProvideCode(provideList.get(i));
            checkLogList.add(checkLog);
        }
        checklogService.saveCheckLogInfo(checkLogList);
        logger.info("插入对账日志记录信息成功====");
        checkLogList.clear();
    }

    /**
     * 对账业务处理
     *
     * @param checkLogs
     * @param billDate
     */
    public void compareRecord(List<CheckLog> checkLogs, String billDate ) {
        resetValue();
        if (checkLogs == null || checkLogs.size() == 0) {
            List<String> provideList = reconciliationService.queryProvide();
            if (provideList != null && provideList.size() != 0) {
                insertData2CheckLog(billDate, provideList, "");
                for (String originProvide : provideList) {
                    //int reconCount = reconciliationService.queryReconCountByProvide(originProvide);
                    compareDeal(billDate, originProvide);
                }
                putCountRecord2Diff(billDate);
            } else {
                provideList = new ArrayList<>();
                provideList.add("93");
                insertData2CheckLog(billDate, provideList, "93");
                logger.info("对账文件中不存在记录信息，插入数据====");
                return;
            }
        } else {
            for (CheckLog checkLog : checkLogs) {
                String originProvide = checkLog.getProvideCode();
                //int reconNum = reconciliationService.queryReconCountByProvide(originProvide);
                compareDeal(billDate, originProvide);
            }
            putCountRecord2Diff(billDate);
        }
        resetValue();
        dealCheckResult(billDate);
        flatAccount(billDate);
    }

    /**
     * 写回执文件
     *
     * @param resultFileName
     * @param checkDay
     */
    public void exportReceipt(String checkDay, String resultFileName) {
        FileWriter fw = null;
        try {
            CheckDiffDataInfo checkDiffDataInfo = checkDiffService.queryDiffTop(checkDay);
            if (checkDiffDataInfo == null) {
                logger.info("未输出回执文件，由于对账文件为存在信息记录=====");
                return;
            } else {
                String failAmount = addZeroForNum(String.valueOf(checkDiffDataInfo.getFailAmount()), 10);
                String failNum = addZeroForNum(String.valueOf(checkDiffDataInfo.getFailNum()), 10);
                String successAmount = addZeroForNum(String.valueOf(checkDiffDataInfo.getSuccessAmount()), 10);
                String successNum = addZeroForNum(String.valueOf(checkDiffDataInfo.getSuccessNum()), 10);
                String unityAmount = addZeroForNum(String.valueOf(checkDiffDataInfo.getUnityAmount()), 10);
                String unityNum = addZeroForNum(String.valueOf(checkDiffDataInfo.getUnityNum()), 10);
                String resultStr = new StringBuffer(unityNum).append(unityAmount).append(successNum).append(successAmount).append
                        (failNum).append(failAmount).append("\r\n").toString();
                fw = new FileWriter(resultFileName, true);
                Set<Integer> provinceSet = abNormalMap.keySet();
                Map parms = new HashMap();
                parms.put("checkDay", checkDay);
                String diffStr = "";
                for (Integer provinceCode : provinceSet) {
                    parms.put("provinceCode", provinceCode);
                    List<CheckDiffDataInfo> checkDiffDataInfos = checkDiffService.queryDiffResultInfo(parms);
                    if (checkDiffDataInfos != null && checkDiffDataInfos.size() != 0) {
                        for (CheckDiffDataInfo diffDataInfo : checkDiffDataInfos) {
                            diffStr = new StringBuffer(diffDataInfo.getDisType()).append(",").append(diffDataInfo.getFinalCharStatus()).
                                    append(",").append(diffDataInfo.getSerialNo()).append("\r\n").toString();
                            fw.write(diffStr);
                        }
                    }
                }
                fw.write(resultStr);
                fw.flush();
                fw.close();
                checkDiffService.updateResultStatus(checkDay);
            }
        } catch (Exception e) {
            logger.info("写回执文件出错====", e);
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (Exception e) {
                logger.info("关闭文件输出流错误====", e);
            }
        }

    }

    /**
     * 字符串固定长度，左补0
     *
     * @param str
     * @param strLength
     * @return
     */
    public String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);// 左补0
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }

    /**
     * 平账，将充值缴费状态不一致的记录平账
     *
     * @param billDate
     */
    private void flatAccount(String billDate) {
        try {
            Map diffParam = new HashMap();
            diffParam.put("checkDay", billDate);
            Set<Integer> provinceSet = abNormalMap.keySet();
            if (provinceSet != null) {
                for (Integer key : provinceSet) {
                    int errorNum = abNormalMap.get(key);
                    if(errorNum < Integer.parseInt(errCount)){
                        diffParam.put("provinceCode", key);
                        List<String> serialNoList = checkDiffService.queryDiffChargeStatus(diffParam);
                        Map params = new HashMap();
                        params.put("checkDay", billDate);
                        if (serialNoList != null && serialNoList.size() != 0) {
                            for (String serialNo : serialNoList) {
                                params.put("serialNo", serialNo);
                                urpChargeinfoService.updateChargeStatus(params);
                                UrpChargeinfo urpChargeinfo = urpChargeinfoService.getChargeInfoByCgiSerialNo(serialNo);
                                String json = JSON.toJSONString(urpChargeinfo);
                                kafkaProducer.send(json);
                            }
                        }
                    }
                }
            }
            updateChargeFail(billDate);
        } catch (Exception e) {
            logger.info("平账异常======", e);
        }
    }

    /**
     * 平账，更新枢纽未传缴费记录，将状态不是成功的改为失败
     *
     * @param billDate
     */
    private void updateChargeFail(String billDate) {

        logger.info("开始更新枢纽未传缴费记录，将状态不是成功的改为失败");
        List<CheckLog> checkLogs = checklogService.querySucCheckInfo(billDate);
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> maps = new HashMap<>();
        if (checkLogs != null && checkLogs.size() != 0) {
            for (CheckLog checkLog : checkLogs) {
                map.put("cardProvince", checkLog.getProvideCode());
                map.put("checkDay", billDate);
                //查询枢纽未传的缴费记录，且状态非成功的流水号
                List<UrpChargeinfo> list = urpChargeinfoService.queryChargeFailure(map);
                //枢纽无，缴费有，且缴费表状态为0成功，将其改为1失败
                List<UrpChargeinfo> lists = urpChargeinfoService.queryChargeZero(map);
                for (UrpChargeinfo ser : list){
                    maps.put("serialNo", ser.getCgiSerialno());
                    urpChargeinfoService.updateChargeFailure(maps);
//                    int errorCount = abNormalMap.get(checkLog.getProvideCode());
//                    errorCount++;
//                    abNormalMap.put(Integer.parseInt(checkLog.getProvideCode()),errorCount);
                }
                for (UrpChargeinfo sers : lists) {
                    maps.put("serialNo", sers.getCgiSerialno());
                    urpChargeinfoService.updateChargeFailure(maps);
//                    int errorCount = abNormalMap.get(checkLog.getProvideCode());
//                    errorCount++;
//                    abNormalMap.put(Integer.parseInt(checkLog.getProvideCode()),errorCount);
                }
            }
        }
    }

    /**
     * 重置异常记录数
     */
    public void resetValue() {
        successCount = 0;
        successAmount = 0;
        failCount = 0;
        failAmount = 0;
        unityCount = 0;
        unityAmount = 0;
    }

    /**
     * 对账信息对比完将异常结果信息处理与日志中的对账状态进行处理
     *
     * @param billDate
     */
    public void dealCheckResult(String billDate) {
        List<String> provideList = reconciliationService.queryProvide();
        for (String provideCode : provideList) {
            int errorCount = abNormalMap.get(Integer.parseInt(provideCode)) == null ? 0 : abNormalMap.get(Integer.parseInt(provideCode));
            logger.info("更新对账状态时获取的异常信息记录==" + errorCount);
            Map params = new HashMap();
            params.put("provideCode", provideCode);
            params.put("checkDay", billDate);
            if (errorCount > Integer.valueOf(errCount)) {
                continue;
            } else {
                checklogService.updateCheckLog(params);
            }
        }
    }

    /**
     * 业务处理子方法
     *
     * @param billDate
     * @param originProvide
     */
    private void compareDeal(String billDate, String originProvide) {
        long c= System.currentTimeMillis();//获取当前系统时间(毫秒)
        long b= System.currentTimeMillis();//获取当前系统时间(毫秒)
        //查询当前逻辑日所有缴费记录
        List<UrpChargeinfo> listAllSerial = urpChargeinfoService.chargeInfoQueryByBillDate(billDate);
        List<String> listAllSerialNo = new ArrayList<>();
        for(UrpChargeinfo urpInfo : listAllSerial){
            listAllSerialNo.add(urpInfo.getCgiSerialno());
        }
        //查询对账表中当前省份所有数据
        List<ReconciliationInfo> allReconciliation = reconciliationService.queryAllReconInfoByProvince(originProvide);
        logger.info("查询对账表中当前省份所有数据执行时间为："+(System.currentTimeMillis()-b)+"毫秒");
        for (ReconciliationInfo reconciliationInfo : allReconciliation){
            int errorCount = abNormalMap.get(reconciliationInfo.getCgiCardprovince()) == null ? 0 : abNormalMap.get(reconciliationInfo
                    .getCgiCardprovince());
            logger.info("省份对账异常记录数=====" + errorCount);
//            UrpChargeinfo urpChargeinfo = urpChargeinfoService.getChargeInfoByCgiSerialNo(reconciliationInfo.getOriginSerialNo());
//            if (urpChargeinfo != null) {
            //当缴费表交易流水号包含当前对账表流水号时比对信息
            if(listAllSerialNo !=null && listAllSerialNo.size()!=0 &&
                    listAllSerialNo.contains(reconciliationInfo.getOriginSerialNo())){
                Map param = new HashMap();
                param.put("checkDay", billDate);
                param.put("serialNo", reconciliationInfo.getOriginSerialNo());
                int footMark = listAllSerialNo.indexOf(reconciliationInfo.getOriginSerialNo());
                String cradNum = listAllSerial.get(footMark).getCiCardnum();
                String accNum = listAllSerial.get(footMark).getCgiAccnum();
                int chargeMoney = listAllSerial.get(footMark).getCgiMoney() * 10;
                if (errorCount > Integer.valueOf(errCount)) {
                    logger.info("异常记录数大于阈值，不做比对记录");
                    return;
                } else {
                    if (!(cradNum.equals(reconciliationInfo.getCgiCardnum()))) {
                        errorCount++;
                        abNormalMap.put(reconciliationInfo.getCgiCardprovince(), errorCount);
                        putChargeData2CheckDiff(billDate, listAllSerial.get(footMark), "F006", 1);
                        failCount++;
                        failAmount += chargeMoney;
                        //若日切字段为空，set当前cutOfDay
                        if("".equals(listAllSerial.get(footMark).getCgiCutOfDay()) || StringUtils.isEmpty(listAllSerial.get(footMark).getCgiCutOfDay())){
                            urpChargeinfoService.updateCheckDay(param);
                        }
                    } else if (!(accNum.equals(reconciliationInfo.getCgiAccnum()))) {
                        errorCount++;
                        abNormalMap.put(reconciliationInfo.getCgiCardprovince(), errorCount);
                        putChargeData2CheckDiff(billDate, listAllSerial.get(footMark), "F001", 1);
                        failCount++;
                        failAmount += chargeMoney;
                        if("".equals(listAllSerial.get(footMark).getCgiCutOfDay()) || StringUtils.isEmpty(listAllSerial.get(footMark).getCgiCutOfDay())){
                            urpChargeinfoService.updateCheckDay(param);
                        }
                    } else if (!(chargeMoney == reconciliationInfo.getCgiMoney())) {
                        errorCount++;
                        abNormalMap.put(reconciliationInfo.getCgiCardprovince(), errorCount);
                        putChargeData2CheckDiff(billDate, listAllSerial.get(footMark), "F003", 1);
                        failCount++;
                        failAmount += chargeMoney;
                        if("".equals(listAllSerial.get(footMark).getCgiCutOfDay()) || StringUtils.isEmpty(listAllSerial.get(footMark).getCgiCutOfDay())){
                            urpChargeinfoService.updateCheckDay(param);
                        }
                    } else if (listAllSerial.get(footMark).getCgiChargestatus() == 1) {
                        errorCount++;
                        abNormalMap.put(reconciliationInfo.getCgiCardprovince(), errorCount);
                        putChargeData2CheckDiff(billDate, listAllSerial.get(footMark), "F002", 0);
                        successCount++;
                        successAmount += chargeMoney;
                    } else {
                        unityCount++;
                        unityAmount += chargeMoney;
                        long a= System.currentTimeMillis();//获取当前系统时间(毫秒)
                        if("".equals(listAllSerial.get(footMark).getCgiCutOfDay()) || StringUtils.isEmpty(listAllSerial.get(footMark).getCgiCutOfDay())){
                            urpChargeinfoService.updateCheckDay(param);
                        }
                        logger.info("updateCheckDay执行时间为："+(System.currentTimeMillis()-a)+"毫秒");
                    }
                }
            } else {
                logger.info("本地充值缴费记录中不存在流水号为" + reconciliationInfo.getOriginSerialNo() + "的记录。");
                logger.info("当前省份编码为：" + reconciliationInfo.getCgiCardprovince());
                UrpChargeinfo urpChargeinfos = new UrpChargeinfo();
                if(reconciliationInfo.getOriginProvince() != 93){
//                    int errorCounts = abNormalMap.get(reconciliationInfo.getCgiCardprovince());
//                    errorCounts++;
//                    abNormalMap.put(reconciliationInfo.getCgiCardprovince(),errorCounts);
                    logger.info(reconciliationInfo.getOriginSerialNo()+"：充值记录表中无该流水号记录，该省份对账异常数为："+abNormalMap.get(reconciliationInfo.getCgiCardprovince()));
                    logger.info("------------开始插入数据--------");
                    urpChargeinfos.setCgiSerialno(reconciliationInfo.getOriginSerialNo());
                    urpChargeinfos.setCgiCallingnum("13333333333");
                    urpChargeinfos.setCgiEntryprovince(reconciliationInfo.getOriginProvince());
                    urpChargeinfos.setCgiEntrytype(reconciliationInfo.getCgiChannelType());
                    urpChargeinfos.setCiCardnum(reconciliationInfo.getCgiCardnum());
                    urpChargeinfos.setCgiCardprovince(reconciliationInfo.getCgiCardprovince());
                    urpChargeinfos.setCgiCardcity(Integer.parseInt(reconciliationInfo.getCgiCardcity()));
                    urpChargeinfos.setCgiAccnum(reconciliationInfo.getCgiAccnum());
                    urpChargeinfos.setCgiAccbasetype(reconciliationInfo.getCgiAccbasetype());
                    urpChargeinfos.setCgiSystemcode(reconciliationInfo.getCgiSystemcode());
                    urpChargeinfos.setCgiAccprovince(Integer.parseInt(reconciliationInfo.getCgiAccprovince()));
                    urpChargeinfos.setCgiAcccity(Integer.parseInt(reconciliationInfo.getCgiAcccity()));
                    urpChargeinfos.setCgiMoney(reconciliationInfo.getCgiMoney());
                    urpChargeinfos.setCgiRequesttime(reconciliationInfo.getTransTime());
                    urpChargeinfos.setCgiChargetime(reconciliationInfo.getTransTime());
                    urpChargeinfos.setCgiRspprocesstime(reconciliationInfo.getDealTime());
                    urpChargeinfos.setCgiChargestatus(0);
                    urpChargeinfos.setCgiRollBackStatus(3);
                    urpChargeinfos.setCgiIsexpcardchg("N");
                    urpChargeinfos.setCgiIsgift(reconciliationInfo.getCgiIsgift());
                    urpChargeinfos.setCgiCardnet(reconciliationInfo.getCgiCardnet());
                    urpChargeinfos.setCgiResendsource("");//TODO:解密 再加密
                    urpChargeinfos.setCgiAcchomeserialno("");
                    urpChargeinfos.setCgiCutOfDay(billDate);
                    urpChargeinfos.setCgiReserve("");
                    urpChargeinfos.setCgiRspcode("");
                    urpChargeinfos.setCgiRspdesc("");
                    urpChargeinfos.setCgiAccBrandCode("");
                    //接入省 93(发起卡) 未割接 号同省 ABB 其他ABC
                    if("93".equals(reconciliationInfo.getOriginProvince()+"")){
                        urpChargeinfos.setCgiChgdirection("SEND");
                        UrpProvinceinfo urpPro = urpProvinceinfoService.provinceinfoQueryById(reconciliationInfo.getOriginProvince()+"");
                        if(urpChargeinfos.getCgiAccprovince()==urpChargeinfos.getCgiCardprovince() && "N".equals(urpPro.getPiIfmigrate())){
                            urpChargeinfos.setCgiChargetype("ABB");
                        }else{
                            urpChargeinfos.setCgiChargetype("ABC");
                        }
                        //接入省非 93 默认发起省已割接 是否卡号同省 ABB
                    }else{
                        urpChargeinfos.setCgiChgdirection("RECV");
                        if(urpChargeinfos.getCgiAccprovince()==urpChargeinfos.getCgiCardprovince()){
                            urpChargeinfos.setCgiChargetype("ABB");
                        }else{
                            urpChargeinfos.setCgiChargetype("ABC");
                        }
                    }
                    logger.info("插入结果："+urpChargeinfos);
                    //插入缴费记录
                    try {
                        urpChargeinfoService.insertChargeinfo(urpChargeinfos);
                        logger.info("缴费记录插入成功");
                    }catch (Exception e) {
                        logger.info("缴费记录插入异常");
                    }
                }
            }
        }
        logger.info("平账执行时间为："+(System.currentTimeMillis()-c)+"毫秒");
    }


    private void putCountRecord2Diff(String billDate) {
        CheckDiffDataInfo checkDiffDataInfo = new CheckDiffDataInfo();
        checkDiffDataInfo.setAccbaseType("");
        checkDiffDataInfo.setAccCity(0);
        checkDiffDataInfo.setAccNum("");
        checkDiffDataInfo.setAccProvince(0);
        checkDiffDataInfo.setCallingNum("9999999999");
        checkDiffDataInfo.setCardCity(0);
        checkDiffDataInfo.setCenterChargeTime("");
        checkDiffDataInfo.setChargeResultDesc("");
        checkDiffDataInfo.setChargeStatus(0);
        checkDiffDataInfo.setChargeTime("");
        checkDiffDataInfo.setChargeType("");
        checkDiffDataInfo.setCheckDay(billDate);
        checkDiffDataInfo.setDisType("");
        checkDiffDataInfo.setEntryProvince(0);
        checkDiffDataInfo.setEntryType("");
        checkDiffDataInfo.setFinalCharStatus(1);
        checkDiffDataInfo.setIsGift("N");
        checkDiffDataInfo.setCardNum("");
        checkDiffDataInfo.setCardProvince(0);
        checkDiffDataInfo.setMoney(0);
        checkDiffDataInfo.setSystemCode("");
        checkDiffDataInfo.setSerialNo(DateUtils.format(new Date(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HHMMSS_SSS));
        checkDiffDataInfo.setSuccessNum(successCount);
        checkDiffDataInfo.setSuccessAmount(successAmount);
        checkDiffDataInfo.setFailNum(failCount);
        checkDiffDataInfo.setFailAmount(failAmount);
        checkDiffDataInfo.setUnityNum(unityCount);
        checkDiffDataInfo.setUnityAmount(unityAmount);
        checkDiffService.saveDiffDataInfo(checkDiffDataInfo);
    }

    /**
     * 枢纽对账信息中记录在充值缴费记录表中不存在记录，将枢纽记录信息存入差异记录中
     *
     * @param billDate
     * @param reconciliationInfo
     */
    /*private void putReconData2CheckDiff(String billDate, ReconciliationInfo reconciliationInfo) {
        CheckDiffDataInfo checkDiffDataInfo = new CheckDiffDataInfo();
        checkDiffDataInfo.setAccbaseType(reconciliationInfo.getCgiAccbasetype());
        checkDiffDataInfo.setAccCity(Integer.parseInt(reconciliationInfo.getCgiAcccity()));
        checkDiffDataInfo.setAccNum(reconciliationInfo.getCgiAccnum());
        checkDiffDataInfo.setAccProvince(Integer.parseInt(reconciliationInfo.getCgiAccprovince()));
        checkDiffDataInfo.setCallingNum(reconciliationInfo.getCgiAccnum());
        checkDiffDataInfo.setCardCity(Integer.parseInt(reconciliationInfo.getCgiCardcity()));
        checkDiffDataInfo.setCenterChargeTime("");
        checkDiffDataInfo.setChargeResultDesc("枢纽有记录而充值没有");
        checkDiffDataInfo.setChargeStatus(0);
        checkDiffDataInfo.setChargeTime(DateUtils.format(reconciliationInfo.getTransTime(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HHMMSS));
        checkDiffDataInfo.setChargeType("");
        checkDiffDataInfo.setCheckDay(billDate);
        checkDiffDataInfo.setDisType("");
        checkDiffDataInfo.setEntryProvince(reconciliationInfo.getOriginProvince());
        checkDiffDataInfo.setEntryType(reconciliationInfo.getCgiChannelType());
        checkDiffDataInfo.setFinalCharStatus(0);
        checkDiffDataInfo.setIsGift(reconciliationInfo.getCgiIsgift());
        checkDiffDataInfo.setCardNum(reconciliationInfo.getCgiCardnum());
        checkDiffDataInfo.setCardProvince(Integer.parseInt(reconciliationInfo.getCgiCardprovince()));
        checkDiffDataInfo.setMoney(reconciliationInfo.getCgiMoney());
        checkDiffDataInfo.setSystemCode(reconciliationInfo.getCgiSystemcode());
        checkDiffDataInfo.setSerialNo(reconciliationInfo.getOriginSerialNo());
        checkDiffService.saveDiffDataInfo(checkDiffDataInfo);
    }*/

    /**
     * 充值缴费记录中存在枢纽传过来的流水号记录
     *
     * @param billDate
     * @param urpChargeinfo
     */
    private void putChargeData2CheckDiff(String billDate, UrpChargeinfo urpChargeinfo, String errorCode, int finalResult) {
        try {
            CheckDiffDataInfo diffDataInfo = checkDiffService.queryDiffInfoBySerialNo(urpChargeinfo.getCgiSerialno());
            logger.info("diffDataInfo=====" + diffDataInfo);
            if (diffDataInfo != null) {
                checkDiffService.deleteDiffInfoBySerialNo(urpChargeinfo.getCgiSerialno());
            }
            CheckDiffDataInfo checkDiffDataInfo = new CheckDiffDataInfo();
            checkDiffDataInfo.setAccbaseType(urpChargeinfo.getCgiAccbasetype());
            checkDiffDataInfo.setAccCity(urpChargeinfo.getCgiAcccity());
            checkDiffDataInfo.setAccNum(urpChargeinfo.getCgiAccnum());
            checkDiffDataInfo.setAccProvince(urpChargeinfo.getCgiAccprovince());
            checkDiffDataInfo.setCallingNum(urpChargeinfo.getCgiCallingnum());
            checkDiffDataInfo.setCardCity(urpChargeinfo.getCgiCardcity());
            checkDiffDataInfo.setCenterChargeTime(urpChargeinfo.getCgiChargetime());
            checkDiffDataInfo.setChargeResultDesc(urpChargeinfo.getCgiRspdesc());
            checkDiffDataInfo.setChargeStatus(urpChargeinfo.getCgiChargestatus());
            checkDiffDataInfo.setChargeTime(urpChargeinfo.getCgiChargetime());
            checkDiffDataInfo.setChargeType(urpChargeinfo.getCgiChargetype());
            checkDiffDataInfo.setCheckDay(billDate);
            checkDiffDataInfo.setDisType(errorCode);
            checkDiffDataInfo.setEntryProvince(urpChargeinfo.getCgiEntryprovince());
            checkDiffDataInfo.setEntryType(urpChargeinfo.getCgiEntrytype());
            checkDiffDataInfo.setFinalCharStatus(finalResult);
            checkDiffDataInfo.setIsGift(urpChargeinfo.getCgiIsgift());
            checkDiffDataInfo.setCardNum(urpChargeinfo.getCiCardnum());
            checkDiffDataInfo.setCardProvince(urpChargeinfo.getCgiCardprovince());
            checkDiffDataInfo.setMoney(urpChargeinfo.getCgiMoney());
            checkDiffDataInfo.setSystemCode(urpChargeinfo.getCgiSystemcode());
            checkDiffDataInfo.setSerialNo(urpChargeinfo.getCgiSerialno());
            checkDiffDataInfo.setSuccessNum(0);
            checkDiffDataInfo.setSuccessAmount(0);
            checkDiffDataInfo.setFailNum(0);
            checkDiffDataInfo.setFailAmount(0);
            checkDiffDataInfo.setUnityNum(0);
            checkDiffDataInfo.setUnityAmount(0);
            checkDiffService.saveDiffDataInfo(checkDiffDataInfo);
        } catch (Exception e) {
            logger.info("插入异常记录出错====", e);
        }

    }


    /**
     * 连接sftp
     *
     * @param sftpUtil
     */

    public static ChannelSftp connectServer(SftpUtil sftpUtil) {
        return sftpUtil.getConnect(FtpConfig.config.get("ftp.ip"), Integer.parseInt(FtpConfig.config.get("ftp.port")), FtpConfig.config
                .get("ftp.userName"), FtpConfig.config.get("ftp.passwd"));
    }

    /**
     * 连接号卡ftp服务器
     *
     * @param sftpUtil
     */

    public static ChannelSftp connectHkServer(SftpUtil sftpUtil) {
        return sftpUtil.getConnect(FtpConfig.config.get("hk.ip"), Integer.parseInt(FtpConfig.config.get("hk.port")), FtpConfig.config.get
                ("hk.userName"), FtpConfig.config.get("hk.passwd"));
    }

    /*public static void main(String[] args) {
        new ReadFileFromFTPTask().run();
    }*/

}
